import { REQUESTLOGIN } from '@/http/interface'
import { GETPERMTREE, GETSUBTREE } from '@/http/system'
import { asynRouter, constantRoutes } from '@/router'
import router from '@/router'
const _ = require('lodash')

function hasPermission(roles, route) {
  //console.log(route)
  if (route.meta && route.meta.menuNo) {
    let o = roles.find((k) => {
      //console.log(k.origin.menuNo +'--'+ route.meta.menuNo)
      //console.log(k)
      if(!k.origin){
        return false;
      }else{
        return k.origin.menuNo == `${route.meta.menuNo}` || route.hidden
      }
    })
    // roles.some(k=>{
    //     k.origin.menuNo == route.meta.menuNo
    // })
    // return roles.some(k => k.origin.menuNo == route.meta.menuNo)

    if (o) {
      return true
    } else {
      return false
    }
    //return roles.some(role => route.origin.menuNo == role.origin.menuNo)
    return true
  } else {
    return true
  }
}

/** 展开数据 */
let arr = []
const unfold = function (list) {
  list.map((k) => {
    arr.push(k)
    if (k.children && k.children.length > 0) {
      unfold(k.children)
    }
  })
}

export function filterAsyncRoutes(routes, roles) {
  const res = []
  routes.forEach((route) => {
    const tmp = { ...route }
    if (hasPermission(roles, tmp)) {
      if (tmp.children) {
        tmp.children = filterAsyncRoutes(tmp.children, roles)
      }
      res.push(tmp)
    }
  })

  return res
}
export default {
  /** 登录 */
  login({ state, commit }, options) {
    REQUESTLOGIN(options).then((res) => {
      if (res.code == 200) {
        let obj = res.data
        sessionStorage.setItem('token', obj.token)
        sessionStorage.setItem('user', JSON.stringify(obj.user))
        commit('SET_TOKEN', res.data)
        _this.$router.push({ name: 'index' })
      }
    })
  },

  /** 退出登录 */
  logout({ state, commit }) {
    commit('SET_TOKEN', '')
    commit('SET_MENU', [])
  },

  /** 获取用菜单权限 */
  getMenu({ state, commit, dispatch }, options) {
    let _this = this
    arr = []
    return new Promise((resolve, reject) => {
      //let list = constantRoutes.concat(asynRouter)
      //dispatch('generateRoutes', res.data);
      //   commit('SET_MENU', list)
      //   resolve(list)
      GETPERMTREE(options)
        .then((res) => {
          if (res.code == 200) {
            dispatch('generateRoutes', res.data).then(msg => {
              resolve(msg)
            })

          }
        })
        .catch((error) => {
          reject(error)
        })
    })
  },

  // 获取页面资源
  getPagePermission({ state, commit }, options) {
    //console.log(options)
    return new Promise((resolve, reject) => {
      GETSUBTREE(options)
        .then((res) => {
          let m = _.flattenDeep(res.data)
          commit('SET_CURRENT_PERMISSION', m)
          resolve(res)
        })
        .catch((error) => {
          reject(error)
        })
    })
  },

  changeSetting({ commit }, data) {
    commit('CHANGE_SETTING', data)
  },

  /** 处理路由 */
  generateRoutes({ commit, state }, list) {
    return new Promise((resolve) => {
      let accessedRoutes
      unfold(list)
      accessedRoutes = filterAsyncRoutes(asynRouter, arr)
      let n = constantRoutes.concat(accessedRoutes)
      commit('SET_PERMISSION', arr)
      if (!state.menu || state.menu.length <= 0) {
        router.addRoutes(n)
      }
      commit('SET_MENU', n)
      resolve(n)
    })
  },

  toggleSideBar({ commit }) {
    commit('TOGGLE_SIDEBAR')
  },
  closeSideBar({ commit }, { withoutAnimation }) {
    commit('CLOSE_SIDEBAR', withoutAnimation)
  },
  toggleDevice({ commit }, device) {
    commit('TOGGLE_DEVICE', device)
  },


}
